﻿// SEARCH_DIA.cpp: 实现文件
//

#include "stdafx.h"
#include "MFCApplication4.h"
#include "SEARCH_DIA.h"
#include "afxdialogex.h"
#include"Date.h"
extern vector<string> split(const string& str, const string& delim);

// SEARCH_DIA 对话框

IMPLEMENT_DYNAMIC(SEARCH_DIA, CDialogEx)

SEARCH_DIA::SEARCH_DIA(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SEARCH, pParent)
{

}

SEARCH_DIA::~SEARCH_DIA()
{
}

void SEARCH_DIA::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO1, RADIO);
	RADIO.SetCheck(true);
	searchmod = "money";
}


BEGIN_MESSAGE_MAP(SEARCH_DIA, CDialogEx)
	ON_BN_CLICKED(IDC_RADIO2, &SEARCH_DIA::OnBnClickedRadio2)
	ON_BN_CLICKED(IDC_BUTTON2, &SEARCH_DIA::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON4, &SEARCH_DIA::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_RADIO1, &SEARCH_DIA::OnBnClickedRadio1)
END_MESSAGE_MAP()


// SEARCH_DIA 消息处理程序


void SEARCH_DIA::OnBnClickedRadio2()
{
	searchmod = "date";
	// TODO: 在此添加控件通知处理程序代码
}


void SEARCH_DIA::OnBnClickedButton2()
{
	vector<string> args;
	vector<string> idlister;
	std::string idList;
	std::string yearIn;
	std::string monIn;
	CString idListc;
	CString yearInc;
	CString monInc;
	GetDlgItemText(IDC_EDIT1, idListc);
	GetDlgItemText(IDC_EDIT3, yearInc);
	GetDlgItemText(IDC_EDIT5, monInc);
	idList = CT2A(idListc.GetString());
	yearIn = CT2A(yearInc.GetString());
	monIn = CT2A(monInc.GetString());
	if(searchmod=="date")
		args.push_back("113");
	else if(searchmod=="money")
		args.push_back("118");

	if (atoi(monIn.c_str()) != 1)
	{
		args.push_back(yearIn);
		args.push_back(to_string(atoi(monIn.c_str())-1));
		args.push_back(to_string(tmDate::maxday(atoi(yearIn.c_str()), atoi(monIn.c_str())-1)));
	}
	else if (atoi(monIn.c_str()) == 1)
	{
			args.push_back(to_string(atoi(yearIn.c_str())-1));
			args.push_back("12");
			args.push_back("31");
	}

	if (atoi(monIn.c_str()) != 12)
	{
		args.push_back(yearIn);
		args.push_back(to_string(atoi(monIn.c_str()) + 1));
		args.push_back("1");
	}
	else if (atoi(monIn.c_str()) == 12)
	{
		args.push_back(to_string(atoi(yearIn.c_str()) + 1));
		args.push_back("1");
		args.push_back("1");
	}

	if (idList == "")
	{
		args.push_back("THEYNEEDALLD");
	}
	else
	{
		idlister=split(idList, "#");
		for (vector<Controller>::iterator it = SystemALL.Center.begin(); it != SystemALL.Center.end(); it++)
		{
			if (it->getUsername() == SystemALL.usernow)
			{
				for (int l = 0; l < idlister.size(); l++)
				{
					args.push_back(idlister[l]);
				}
			}
		}
	}
	try
	{
		for (vector<Controller>::iterator it = SystemALL.Center.begin(); it != SystemALL.Center.end(); it++)
		{
			if (it->getUsername() == SystemALL.usernow)
			{
				it->getCommand(args, SystemALL.nDate);
			}
		}
	}
	catch (runtime_error &e)
	{
		string tmp = e.what();
		tmp += "\r\n";
		SystemALL.WriteIntoEditer(tmp);
	}
	// TODO: 在此添加控件通知处理程序代码
}


void SEARCH_DIA::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	EndDialog(0);
}


void SEARCH_DIA::OnBnClickedRadio1()
{
	searchmod = "money";

	// TODO: 在此添加控件通知处理程序代码
}
